#include "mainwindow.h"
#include <QApplication>
#include "logindialog.h"
#include <QtDebug>
#include <QFile>
#include <QTextStream>
#include <keypadhandler.h>
#include <QDesktopWidget>
#include <qdatetime.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <QScreen>
#include <QWSServer>
#include <QTextCodec>
#include <uguiscreen.h>
#include <QThread>
#include <QFontDatabase>
#include <QFont>

void customMessageHandler(QtMsgType type, const char *msg)
{
    QString txt;
    switch (type)
    {
    //调试信息提示
    case QtDebugMsg:
        txt = QString("Debug [%1]: %2").arg(QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss.zzz"),msg);
        break;

        //一般的warning提示
    case QtWarningMsg:
        txt = QString("Warning [%1]: %2").arg(QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss.zzz"),msg);
        break;

        //严重错误提示
    case QtCriticalMsg:
        txt = QString("Critical [%1]: %2").arg(QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss.zzz"),msg);
        break;

        //致命错误提示
    case QtFatalMsg:
        txt = QString("Fatal [%1]: %2").arg(QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss.zzz"),msg);
        abort();
    }
    QFile outFile("/home/eposlog/log"+QDateTime::currentDateTime().toString("yyyyMMdd")+".txt");
    outFile.open(QIODevice::WriteOnly | QIODevice::Append);
    QTextStream ts(&outFile);
    ts << txt << endl;
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    // add font
    QTextCodec *codec = QTextCodec::codecForName("UTF-8");
    QTextCodec::setCodecForCStrings(codec);
    QTextCodec::setCodecForTr(codec);
    QTextCodec::setCodecForLocale(codec);
    a.setFont(QFont("wenquanyi", 12, QFont::Normal));

    QCoreApplication::setOrganizationName("LorytechEPOS");
    QCoreApplication::setOrganizationDomain("lorytech.com");
    QCoreApplication::setApplicationName("EPOS");

#if defined(Q_WS_QWS)
    QWSServer::setCursorVisible(false);
#endif

    qInstallMsgHandler(customMessageHandler);

    QDesktopWidget* desktop=QApplication::desktop();


    qDebug()<<"init screen width"<<desktop->width();
    qDebug()<<"init screen count"<<desktop->screenCount();

#if defined(Q_WS_QWS)
    KeypadHandler *key=new KeypadHandler();
    qDebug()<<"init keyboard"<<key->objectName();
#endif
    MainWindow w;
    w.show();
    //QThread *thread = new QThread();
    //thread->start();
    //SecondGUI_Test();
    //SecondGUI_Test();

    //LoginDialog dlg;
    //dlg.show();
    return a.exec();

    /**
    if (dlg.exec() == QDialog::Accepted)
    {
        qDebug()<<"login accept";
        w.show();
        return a.exec();
    }
    else return 0;
    **/


}

void closeApp(){

}
